//
//  String.swift
//
//  Created by 李加建 on 2021/9/7.
//

import Foundation

public extension String {
    func toDate(_ dateFormat: String = "yyyy-MM-dd HH:mm:ss") -> Date? {
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "zh_CN")
        formatter.dateFormat = dateFormat
        let date = formatter.date(from: self)
        return date
    }

    func timeAgo() -> String? {
        toDate()?.timeAgo()
    }

    func timeFormat(_ dateFormat: String = "yyyy-MM-dd HH:mm:ss") -> String? {
        toDate()?.toString(dateFormat)
    }

    func timeFormatYMD() -> String? {
        toDate()?.toString("yyyy-MM-dd")
    }

    func timeFormatYMDHM() -> String? {
        toDate()?.toString("yyyy-MM-dd HH:mm")
    }
}
